/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Persistencia;

//import Clases.Estudio55;
//import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import java.util.ArrayList;
//import java.util.Iterator;
import java.util.List;

/**
 *
 * @author Katz - De la Torre
 */
public class persistencia {

    private SessionFactory sessionFactory;
    private Session session;
    //private Restaurant restaurant;

    public persistencia() {
        sessionFactory = new Configuration().configure("/hibernate.cfg.xml").buildSessionFactory();
        session = sessionFactory.openSession();

    }

    public void insert(Object unObjeto) {
        Transaction tr = session.beginTransaction();
        session.save(unObjeto);
        tr.commit();
    }

    public void update(Object unObjeto) {
        Transaction tr = session.beginTransaction();

        session.update(unObjeto);
        tr.commit();
    }

    public List getClientes() throws ArrayStoreException {
        String hql = "from Cliente";
        List items = new ArrayList();
        Transaction tx = session.beginTransaction();
        Query q = session.createQuery(hql);
        items = q.list();
        tx.commit();
        return items;
    }

    public List getUsuarios() throws ArrayStoreException {
        String hql = "from Usuarios";
        List items = new ArrayList();
        Transaction tx = session.beginTransaction();
        Query q = session.createQuery(hql);
        items = q.list();
        tx.commit();
        return items;
    }

    public List getUsuarios(String nombre) throws ArrayStoreException {
        String hql = "from Usuarios where NombreUsuario='" + nombre + "'";
        List items = new ArrayList();
        Transaction tx = session.beginTransaction();
        Query q = session.createQuery(hql);
        items = q.list();
        tx.commit();
        return items;
    }

    public List getEmpresas() throws ArrayStoreException {
        String hql = "from Empresa";
        List items = new ArrayList();
        Transaction tx = session.beginTransaction();
        Query q = session.createQuery(hql);
        items = q.list();
        tx.commit();
        return items;
    }

    public List getCategorias() throws ArrayStoreException {
        String hql = "from Categorias";
        List items = new ArrayList();
        Transaction tx = session.beginTransaction();
        Query q = session.createQuery(hql);
        items = q.list();
        tx.commit();
        return items;
    }

    public List getBoliches() throws ArrayStoreException {
        String hql = "from Boliche";
        List items = new ArrayList();
        Transaction tx = session.beginTransaction();
        Query q = session.createQuery(hql);
        items = q.list();
        tx.commit();
        return items;
    }

    public List getUnaEmpresa(String DescripcionEmpresa) throws ArrayStoreException {
        String hql = "from Empresa where Descripcion='" + DescripcionEmpresa + "'";
        List items = new ArrayList();
        Transaction tx = session.beginTransaction();
        Query q = session.createQuery(hql);
        items = q.list();
        tx.commit();
        return items;
    }

    public List getUnaCategoria(String DescripcionCategoria) throws ArrayStoreException {
        String hql = "from Categorias where Descripcion='" + DescripcionCategoria + "'";
        List items = new ArrayList();
        Transaction tx = session.beginTransaction();
        Query q = session.createQuery(hql);
        items = q.list();
        tx.commit();
        return items;
    }
//    public List getMesas() throws ArrayStoreException{
//        String hql="from Mesa";
//        List items= new ArrayList();
//        Transaction tx = session.beginTransaction();
//        Query q=session.createQuery(hql);
//        items = q.list();
//        tx.commit();
//        return items;
//    }
//
//    public List getReservas() throws ArrayStoreException{
//        String hql="from Reserva";
//        List items= new ArrayList();
//        Transaction tx = session.beginTransaction();
//        Query q=session.createQuery(hql);
//        items = q.list();
//        tx.commit();
//        return items;
//    }
    
    public List getUnUsuario(String NombreUsuario) throws ArrayStoreException {
        String hql = "from Usuarios where NombreUsuario='" + NombreUsuario + "'";
        List items = new ArrayList();
        Transaction tx = session.beginTransaction();
        Query q = session.createQuery(hql);
        items = q.list();
        tx.commit();
        return items;
    }

}
